
//直接使用这两个map会导致有值但是size为0，因为数据来源是异步请求来的，无法正常使用
//需要改成同步执行
let typeMap = new Map;
let chapterMap = new Map;

async function getType(){
    let typeSelect = $("#type");
    $.ajax({
        type: "GET",
        url: "webapi/exercise/getAllType",
    }).done(function (data){
        for (const key in data) {
            let type = data[key];
            let option = $("<option value=" + type.id + ">" + type.name + "</option>");
            typeMap.set(type.id, type.name);
            typeSelect.append(option);
        }
    });
}

async function getChapter() {
    let chapterSelect = $("#chapter");
    $.ajax({
        type: "GET",
        url: "webapi/exercise/getAllChapter",
    }).done(function (data){
        for (const key in data) {
            let chapter = data[key];
            let option = $("<option value=" + chapter.id + ">" + chapter.name + "</option>");
            chapterMap.set(chapter.id, chapter.name);
            chapterSelect.append(option);
        }
    });
}

function deleteExercise(id) {
    //console.log("id=" + id);
    if (confirm("确认删除此习题？")){
        $.ajax({
            type: "POST",
            url: "webapi/exercise/deleteExercise",
            data: {
                id: id
            }
        }).done(function (data){
            if (data === 1){
                alert("删除成功");
                loadTable(0);
            } else {
                alert("删除失败");
            }
        });
    }
}

function loadTable(page) {

    //console.log("调用了loadTable方法");

    let title = $("#title").val().trim();
    let type = $("#type").val();
    let chapter = $("#chapter").val();
    $.ajax({
        type: "GET",
        url: "webapi/exercise/getByPage",
        data: {
            page:page,
            size:3,
            title:title,
            type:type,
            chapter:chapter
        }
    }).done(function (pageUtil){
        //console.log(pageUtil);
        let table = $("#table");
        let pagination = $("#pagination");
        //先清空表格
        table.empty();
        pagination.empty();

        let map = pageUtil.map;
        //console.log(map);
        for (const key in map) {
            let exercise = setExercise(key);
            //console.log(typeof exercise.get('chapter'));

            let id = $("<td>" + exercise.get('id') + "</td>");
            let title =$("<td style='width: 250px'>" + exercise.get('title') + "</td>");
            let answer = $("<td>" + exercise.get('answer') + "</td>");
            let type = $("<td>" + typeMap.get(Number(exercise.get('type'))) + "</td>");
            let chapter = $("<td>" + chapterMap.get(Number(exercise.get('chapter'))) + "</td>");

            let options = $("<dl></dl>");
            let optionA = $("<dd>A." + map[key].optionA + "</dd>");
            let optionB = $("<dd>B." + map[key].optionB + "</dd>");
            let optionC = $("<dd>C." + map[key].optionC + "</dd>");
            let optionD = $("<dd>D." + map[key].optionD + "</dd>");
            options.append(optionA).append(optionB).append(optionC).append(optionD);

            let btn = $("<td>" +
                "<a class='button primary small' href='/toUpdateExercise?id=" + exercise.get('id') + "'>修改</a>" +
                "<a class='button small' onclick='deleteExercise(" + exercise.get('id') + ")'>删除</a>" +
                "</td>");

            let exerciseInfo = $("<tr></tr>").append(id).append(title).append(options).append(answer).append(type).append(chapter)
                .append(btn);
            table.append(exerciseInfo);
        }
        //页码处理
        let current = pageUtil.current;
        let totalPage = pageUtil.totalPage;
        let total = pageUtil.total;
        pagination.append("<li><span class='button small' onclick='loadTable(0)'>首页</span></li>")
        if (current > 0){
            pagination.append("<li><span class='button small' onclick='loadTable(" + (current - 1) + ")'>上一页</span></li>");
        }
        for (let i = 0; i < totalPage; i++) {
            let pageNum = $("<li><span class='page' onclick='loadTable(" + i + ")'>" + (i + 1) + "</span></li>");
            if (i === current){
                pageNum.children("span").addClass("active");
            }
            pagination.append(pageNum);
        }
        if (current < totalPage - 1){
            pagination.append("<li><span class='button small' onclick='loadTable(" + (current + 1) + ")'>下一页</span></li>");
        }
        pagination.append("<li><span class='button small' onclick='loadTable(" + (totalPage - 1) + ")'>尾页</span></li>");
        pagination.append("<li>共找到" + total + "个题目" + "</li>");
    })
}